package service.impl;

import dao.LabelDao;
import dao.NoteLabelsDao;
import dao.impl.LabelDaoImpl;
import dao.impl.NoteLabelsDaoImpl;
import pojo.Label;
import pojo.NoteLabels;
import service.LabelService;
import service.NoteLabelsService;

import java.sql.SQLException;
import java.util.ArrayList;

public class NoteLabelsServiceImpl implements NoteLabelsService {
    private boolean flag = false;


    public boolean addNoteLabels(NoteLabels noteLabels) throws SQLException {
        NoteLabelsDao noteLabelsDao = new NoteLabelsDaoImpl();
        flag = noteLabelsDao.addNoteLabels(noteLabels);
        return flag;
    }


    public ArrayList<NoteLabels> noteLabelsList(Integer nid) throws SQLException, ClassNotFoundException {
        NoteLabelsDao noteLabelsDao = new NoteLabelsDaoImpl();
        ArrayList<NoteLabels> noteLabelsList = new ArrayList<>();
        noteLabelsList = noteLabelsDao.noteLabelsList(nid);
        return noteLabelsList;

    }


    public ArrayList<Label> noteLabels(ArrayList<NoteLabels> noteLabelsList) throws SQLException {
        ArrayList<Label> labelList = new ArrayList<>();
        LabelService labelService = new LabelServiceImpl();
        String label = null;
        Integer lid = 0;
        for (NoteLabels noteLabels : noteLabelsList) {
            label = labelService.findLabelById(noteLabels.getLid());
            lid = noteLabels.getLid();
            Label eachLabel = new Label();
            eachLabel.setLabel(label);
            eachLabel.setId(lid);
            labelList.add(eachLabel);
        }
        return labelList;
    }


    public ArrayList<NoteLabels> searchAllLabels(String content) throws SQLException, ClassNotFoundException {
        ArrayList<Label> afterSearchTotalLabel = new ArrayList<>();
        LabelDao labelDao = new LabelDaoImpl();
        afterSearchTotalLabel = labelDao.afterSearchTotalLabel(content);
        NoteLabelsDao noteLabelsDao = new NoteLabelsDaoImpl();
        ArrayList<NoteLabels> searchNoteLabels = new ArrayList<>();
        searchNoteLabels = noteLabelsDao.searchNoteLabels(afterSearchTotalLabel);
        return searchNoteLabels;
    }


    public ArrayList<NoteLabels> exatSearchAllLabels(String labelSearch) throws SQLException, ClassNotFoundException {
        ArrayList<Label> afterSearchTotalLabel = new ArrayList<>();
        LabelDao labelDao = new LabelDaoImpl();
        NoteLabelsDao noteLabelsDao = new NoteLabelsDaoImpl();
        Integer lid = labelDao.findLabelLid(labelSearch);
        ArrayList<NoteLabels> exatSearchNoteLabels = new ArrayList<>();
        exatSearchNoteLabels = noteLabelsDao.findNoteLabelsByLid(lid);
        return exatSearchNoteLabels;
    }
}
